package jcu.ict.profaid;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class ViewFlipperMainActivity extends Activity {
	private ViewFlipper viewFlipper;
	private float lastX;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		int gallery_grid_Images[] = { R.drawable.addpolling,
				R.drawable.phonenumber, R.drawable.result, R.drawable.display,
				R.drawable.longpressquestion, R.drawable.listquestiom,
				R.drawable.addquestion, R.drawable.listpolling };

		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_flipper_main);
		viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);

		for (int i = 0; i < gallery_grid_Images.length; i++) {
			// This will create dynamic image view and add them to ViewFlipper
			setFlipperImage(gallery_grid_Images[i]);
		}
	}

	private void setFlipperImage(int res) {
		Log.i("Set Filpper Called", res + "");
		ImageView image = new ImageView(getApplicationContext());
		image.setBackgroundResource(res);
		viewFlipper.addView(image);
		viewFlipper.setPadding(100, 0, 100, 0);
	}

	// Method to handle touch event like left to right swap and right to left
	// swap
	public boolean onTouchEvent(MotionEvent touchevent) {
		switch (touchevent.getAction()) {
		// when user first touches the screen to swap
		case MotionEvent.ACTION_DOWN: {
			lastX = touchevent.getX();
			break;
		}
		case MotionEvent.ACTION_UP: {
			float currentX = touchevent.getX();

			// if left to right swipe on screen
			if (lastX < currentX) {
				// If no more View/Child to flip
				if (viewFlipper.getDisplayedChild() == 0)
					break;

				viewFlipper.setInAnimation(this, R.anim.in_from_left);
				viewFlipper.setOutAnimation(this, R.anim.out_to_right);
				// Show the next Screen
				viewFlipper.showNext();
			}

			// if right to left swipe on screen
			if (lastX > currentX) {
				if (viewFlipper.getDisplayedChild() == 1)
					break;
				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Right and current Screen
				// will go OUT from Left
				viewFlipper.setInAnimation(this, R.anim.in_from_right);
				viewFlipper.setOutAnimation(this, R.anim.out_to_left);
				// Show The Previous Screen
				viewFlipper.showPrevious();
			}
			break;
		}
		}
		return false;
	}

}
